package cn.px.rest.controller.index;

import cn.px.common.model.UserInfo;
import cn.px.common.util.R;
import cn.px.common.util.SecurityUtils;
import cn.px.dao.enums.CheckStandardTypeEnum;
import cn.px.dao.enums.SysRoleCodeEnum;
import cn.px.dao.vo.index.GatherTaskVO;
import cn.px.service.check.CheckJobService;
import cn.px.service.repair.RepairApplicationFormService;
import cn.px.service.sb.SbOilService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description 生产商 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/index")
public class IndexController {

    private final SbOilService sbOilService;

    private final CheckJobService checkJobService;

    private final RepairApplicationFormService repairApplicationFormService;

    /**
     * 统计用户的任务
     * 当日的任务（截止到当前日期未完成的）
     * 1周的任务
     * 1个月的任务
     *
     * @return R
     */
    @GetMapping("/gather/task/user")
    public R gatherTaskUser() {
        UserInfo userInfo = SecurityUtils.getUserInfo();
        List<String> roleCodes = userInfo.getRoleCodes();
        boolean isOperator = false;
        for(String roleCode:roleCodes){
            System.out.println("roleCode: " + roleCode);
            if(roleCode.equals(SysRoleCodeEnum.Opreator.name())){
                isOperator = true;
                break;
            }
        }
        // 当日,本周，本月待保养任务
        int lubricationTask = checkJobService.countTask(null, userInfo.getUserId(), CheckStandardTypeEnum.POLLING.getValue(), 1);
        int lubricationTaskWeek = checkJobService.countTask(null, userInfo.getUserId(), CheckStandardTypeEnum.POLLING.getValue(), 2);
        int lubricationTaskMonth = checkJobService.countTask(null, userInfo.getUserId(), CheckStandardTypeEnum.POLLING.getValue(), 3);

        // 维修任务：所有的未完成的,登录用户需要完成的
        int repairTask = repairApplicationFormService.countTask(null, userInfo.getUserId(), 2);

        // 维修验收任务
        int repairCheckTask = repairApplicationFormService.countTask(null, userInfo.getUserId(), 3);
        return R.success(new GatherTaskVO()
                .setLubricationTask(lubricationTask)
                .setLubricationTaskWeek(lubricationTaskWeek)
                .setLubricationTaskMonth(lubricationTaskMonth)
                .setRepairTask(repairTask).setRepairCheckTask(repairCheckTask));
    }

    /**
     * 根据身份统计任务：当日（一直到当天未完成的）
     * 1：维修人员：自己的任务，关于某一台设备
     * 2：管理人员：设备所有的任务
     *
     * @return R
     */
    @GetMapping("/gather/task/role/{sbId}")
    public R gatherTaskRole(@PathVariable(value = "sbId", required = false) String sbId) {
        UserInfo userInfo = SecurityUtils.getUserInfo();
        // 当日,本周，本月待保养任务
        int lubricationTaskMyDay = checkJobService.countSbUserTask(sbId, userInfo.getUserId(), CheckStandardTypeEnum.POLLING.getValue());
        int lubricationTaskAllDay = 0;
        if(SecurityUtils.isRole(SysRoleCodeEnum.MM.name())){
            lubricationTaskAllDay = checkJobService.countSbUserTask(sbId, null, CheckStandardTypeEnum.POLLING.getValue());
        }
        return R.success(new GatherTaskVO()
                .setLubricationTaskMyDay(lubricationTaskMyDay).setLubricationTaskAllDay(lubricationTaskAllDay));

    }

}
